﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hos.ScheduleMaster.Core.Common;
using Hos.ScheduleMaster.Core.Interface;
using Hos.ScheduleMaster.Core.Models;

namespace Hos.ScheduleMaster.Core.Services
{

    [ServiceMapTo(typeof(IScheduleGroupService))]
    public class ScheduleGroupService : BaseService, IScheduleGroupService
    {
        /// <summary>
        /// 查询分组分页数据
        /// </summary>
        /// <param name="pager"></param>
        /// <returns></returns>
        public ListPager<ScheduleGroupEntity> GetScheduleGroupPager(ListPager<ScheduleGroupEntity> pager)
        {
            return _repositoryFactory.ScheduleGroups.WherePager(pager, t => true, t => t.CreateTime, false);
        }

        /// <summary>
        /// 根据id查询分组
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ScheduleGroupEntity GetScheduleGroupById(string id)
        {
            return _repositoryFactory.ScheduleGroups.FirstOrDefault(x => x.Id == id);
        }
        /// <summary>
        /// 判断组名是否已存在
        /// </summary>
        /// <param name="groupName"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool CheckScheduleGroupName(string groupName, string id = "")
        {
            if (!string.IsNullOrWhiteSpace(id))
            {
                var model = GetScheduleGroupById(id);
                if (model != null && model.GroupName.ToLower() == groupName.ToLower())
                {
                    return true;
                }
            }
            return _repositoryFactory.ScheduleGroups.FirstOrDefault(x => x.GroupName.ToLower() == groupName.ToLower()) == null;
        }

        /// <summary>
        /// 新增分组
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool AddScheduleGroup(ScheduleGroupEntity model)
        {
            model.Id = Guid.NewGuid().ToString("N");
            model.CreateTime = DateTime.Now;
            _repositoryFactory.ScheduleGroups.Add(model);
            return _unitOfWork.Commit() > 0;
        }

        /// <summary>
        /// 编辑分组信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool EditScheduleGroup(ScheduleGroupEntity model)
        {
            _repositoryFactory.ScheduleGroups.UpdateBy(x => x.Id == model.Id, new
            {
                model.GroupName,
                model.Remark
            });
            return _unitOfWork.Commit() > 0;
        }

        /// <summary>
        /// 查询所有分组
        /// </summary>
        /// <returns></returns>
        public List<ScheduleGroupEntity> GetScheduleGroupAll()
        {
            return _repositoryFactory.ScheduleGroups.WhereOrderBy(m => true, m => m.CreateTime, false).ToList();
        }

        /// <summary>
        /// 检查能否删除分组
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool CanDelete(string id)
        {
            return !_repositoryFactory.Schedules.Any(t => t.GroupId == id);
        }
        /// <summary>
        /// 删除分组
        /// </summary>
        /// <param name="id"></param>
        public bool DeleteGroupd(string id)
        {
            _repositoryFactory.ScheduleGroups.DeleteBy(t => t.Id == id);

            return _unitOfWork.Commit() > 0;
        }
    }
}
